In [1]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

This is a response to the blog on finding peaks in Python: Peak detection in the Python world

The findpeaks.py can be found here: https://github.com/jankoslavic/py-tools/findpeaks


In [2]:
from findpeaks import findpeaks

In [4]:
n = 80
m = 20
limit = 0
spacing = 3
t = np.linspace(0., 1, n)
x = np.zeros(n)
np.random.seed(0)
phase = 2 * np.pi * np.random.random(m)
for i in range(m):
    x += np.sin(phase[i] + 2 * np.pi * t * i)

peaks = findpeaks(x, spacing=spacing, limit=limit)
plt.plot(t, x)
plt.axhline(limit, color='r')
plt.plot(t[peaks], x[peaks], 'ro')
plt.title('Peaks: minimum value {limit}, minimum spacing {spacing} points'.format(**{'limit': limit, 'spacing': spacing}))
plt.show()



In [5]:
%%timeit
peaks = findpeaks(x, spacing=100, limit=4.)


1000 loops, best of 3: 323 µs per loop